﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace B121B {
    /// <summary>
    /// Seller class
    /// </summary>
    public class Seller {
        /// <summary>
        /// The seller class
        /// </summary>
        /// <param name="name">Name of the seller</param>
        /// <param name="zipCode">Where the seller lives</param>
        /// <param name="phone">Sellers phone number</param>
        /// <param name="webPage">Sellers webpage</param>
        public Seller(string name, string zipCode, string phone, string webPage) {
            Name = name;
            ZipCode = zipCode;
            Phone = phone;
            WebPage = webPage;
            VehiclesForSale = new List<Vehicle>();
        }

        #region Properties
        /// <summary>
        /// Sellers name
        /// </summary>
        public string Name {
            get { return _Name; }
            set {
                if (value == null) { // Null check
                    throw new ArgumentNullException("A name cannot be null");
                } else if (value == "") {
                    throw new ArgumentException("A name cannot be empty");
                } else {
                    _Name = value;
                }
            }
        }
        private string _Name;

        /// <summary>
        /// Where the seller lives
        /// </summary>
        public string ZipCode {
            get { return _ZipCode; }
            set {
                int tempVar;
                if (value == null) { // Null check
                    throw new ArgumentNullException("A zipcode cannot be null");
                } else if (value.Length != 4) { // Check length
                    throw new ArgumentException("Bad number");
                } else if (!int.TryParse(value, out tempVar)) { // Check if it is a number
                    throw new ArgumentException("Value should be a number");
                } else {
                    _ZipCode = value;
                }
            }
        }
        private string _ZipCode;

        /// <summary>
        /// Sellers phone number
        /// </summary>
        public string Phone {
            get { return _Phone; }
            set {
                int tempVar;
                if (value == null) { // Null check
                    throw new ArgumentNullException("A phone number cannot be null");
                } else if (value.Length != 8) { // Checks the length of the number (must be 8 according to the assignment)
                    throw new FormatException("Bad number");
                } else if (!int.TryParse(value, out tempVar)) { // Checks if the number is a valid number
                    throw new ArgumentException("Value should be a number");
                } else {
                    _Phone = value;
                }
            }
        }
        private string _Phone;

        /// <summary>
        /// Sellers webpage
        /// </summary>
        public string WebPage {
            get { return _WebPage; }
            set {
                if (value.StartsWith("http://www")) { // value is okay
                    _WebPage = value;
                } else if (value.StartsWith("www")) { // value lacks http://
                    _WebPage = "http://" + value;
                } else if (value.StartsWith("http://")) { // value lacks www
                    _WebPage = value.Insert(7, "www.");
                } else { // value lacks http://www
                    _WebPage = "http://www." + value;
                }
            }
        }
        private string _WebPage;

        /// <summary>
        /// The vehicles for sale
        /// </summary>
        public List<Vehicle> VehiclesForSale { get; set; }
        #endregion

        public override string ToString() {
            return Name + Environment.NewLine +
                "  Zip: " + ZipCode + Environment.NewLine +
                "  Phone: " + Phone + Environment.NewLine +
                "  WebPage: " + WebPage + Environment.NewLine +
                "  # vehicles: " + VehiclesForSale.Count;
        }
    }
}
